package love.yuanmeng.guetcoalsystem.cotroller;/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:zjj
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/10/9 23:27
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import love.yuanmeng.guetcoalsystem.pojo.User;
import love.yuanmeng.guetcoalsystem.vo.NewsAndMessage;
import love.yuanmeng.guetcoalsystem.vo.NewsAndPage;
import love.yuanmeng.guetcoalsystem.pojo.News;
import love.yuanmeng.guetcoalsystem.service.NewsService;
import love.yuanmeng.guetcoalsystem.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @ResponseBody
    @RequestMapping("addNew")
    public ResultObj addNew(News addForm,  MultipartFile file,HttpServletRequest request){
        boolean b = newsService.addNew(addForm, file);
        if(b){
            return ResultObj.ADD_SUCCESS;
        }else {
            return ResultObj.ADD_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("editNew")
    public ResultObj editNew(News editForm, @RequestParam("file") MultipartFile file,HttpServletRequest request){
        boolean b = newsService.editNew(editForm, file);
        return b==true?ResultObj.UPDATE_SUCCESS:ResultObj.UPDATE_ERROR;
    }

    @ResponseBody
    @RequestMapping("editNewNllImg")
    public ResultObj editNewNllImg(@RequestBody News news,HttpServletRequest request){
        news.setNewImg(null);
        boolean b = newsService.editNewNullImg(news);
        return b==true?ResultObj.UPDATE_SUCCESS:ResultObj.UPDATE_ERROR;
    }

    @ResponseBody
    @RequestMapping("deleNew")
    public ResultObj deleNew(@RequestBody News news){
        boolean b = newsService.deleNew(news)==1;
        return b==true?ResultObj.DELETE_SUCCESS:ResultObj.DELETE_ERROR;
    }


    @ResponseBody
    @RequestMapping("getNews")
    public IPage getNews(@RequestBody NewsAndPage newsAndPage){
//    public IPage getNews(@RequestBody NewsAndPage newsAndPage,@RequestParam Integer isAudit){
        System.out.println(newsAndPage.getNews().getUserid());
        return newsService.getNews(newsAndPage);
    }

    @ResponseBody
    @RequestMapping("getRecent")
    public List<News> getRecent(@RequestBody News news){
        return newsService.getRecent(news);
    }

    @ResponseBody
    @RequestMapping("auditNews")
    public ResultObj auditNews(@RequestBody NewsAndMessage newsAndMessage){
        boolean b = newsService.auditNews(newsAndMessage.getNews(), newsAndMessage.getMessage());
        return b?ResultObj.UPDATE_SUCCESS:ResultObj.UPDATE_ERROR;
    }


}
